import {Component} from '@angular/core';
import {
    AlertController, LoadingController, NavController, NavParams, Platform,
    ToastController, ViewController
} from 'ionic-angular';
import {BasePage} from "../../BasePage";
import {SystemService} from "../../service/SystemService";
import {Log} from "../../../framework/utils/Log";
import {WebUrl} from "../../../framework/interface/webUrl";
import {Utils} from "../../../framework/utils/Utils";
import {SLLocalStorageUtils} from "../../../framework/utils/SLLocalStorageUtils";

/**
 * Generated class for the LoginSettingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-login-setting',
    templateUrl: 'login-setting.html',
    providers: [SystemService]
})
export class LoginSettingPage extends BasePage {
    private static TAG: string = "LoginSettingPage";

    public server: { ip?: string, port?: string } = {
        ip: WebUrl.ip || '43.138.239.123' ,
        port: WebUrl.port
    };

    constructor(public navCtrl: NavController, public navParams: NavParams, public sys: SystemService,
                public platform: Platform,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController, public view: ViewController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
    }

    async ionViewDidLoad() {
        Log.w('Tag', 'ionViewDidLoad LoginSettingPage');
        try {
            this.server.ip = Utils.isNullOrUndefined(await SLLocalStorageUtils.getInstance().get_Ip())
                ? WebUrl.ip : await SLLocalStorageUtils.getInstance().get_Ip();
            this.server.port = Utils.isNullOrUndefined(await SLLocalStorageUtils.getInstance().get_Port())
                ? WebUrl.port : await SLLocalStorageUtils.getInstance().get_Port();
            Log.d(LoginSettingPage.TAG, "this.server.ip == " + this.server.ip);
            Log.d(LoginSettingPage.TAG, "this.server.port== " + this.server.port);
        } catch (error) {
            Log.printObj(LoginSettingPage.TAG, "error == ", error);
        }
    }

    async settingPort() {
        Log.w(LoginSettingPage.TAG, "WebUrl.baseUrl=" + WebUrl.baseUrl);
        WebUrl.setIpAndPost(this.server.ip, this.server.port);
        WebUrl.baseUrl='http://'+this.server.ip+':'+this.server.port;
        WebUrl.baseSetting(WebUrl.baseUrl);
        try {
            await SLLocalStorageUtils.getInstance().saveUrl(this.server.ip, this.server.port);

        } catch (error) {
            Log.printObj(LoginSettingPage.TAG, "error == ", error);
            throw error;
        }
    }

   async save() {
        if(this.server.ip =="" || this.server.port==""){
            this.showError('IP或者端口不能为空');
        }else {
           await this.settingPort();
            this.showLoading(true, '正在设置');
            this.sys.getIpPortInfo(this.server).subscribe((res: any) => {
                if (res.msgCode == '1') {
                    this.showLoading(false, '');
                    this.showToast('设置成功');
                    this.bgConClick();
                } else {
                    this.showLoading(false, '');
                    this.showToast('设置失败');
                }

                Log.w(LoginSettingPage.TAG, res);
            }, error => {
                this.showLoading(false, '');
                let err = JSON.stringify(error)
                this.showToast(err);
            });
        }

    }
    bgConClick(){

        if(this.view){
            this.view.dismiss(null, '', {animate: false});
        }
        event.stopPropagation();
    }
}
